/*******************************************************************************
Define globals and locals
********************************************************************************/
capture log close
global input_current_data = <>
global logdir  = <>

local date = string(date(c(current_date),"DMY"), "%tdCCYYNNDD")
log using "$logdir/summarize_ptc_`date'.log", replace


forvalues yy = 22/23 {

	global y = `yy'

	use $input_current_data$y.dta, replace

	/*generate filing status groupings we care about*/
	g single = filing_status ==1 & tax_famsize==1
	g parent_1kid = (filing_status == 4 | filing_status==1) & tax_famsize==2
	g nonrec = f8962==0
	
	
	/******************************************************************************
	*Generate hypothetical percent fpls and credits if you are a filer or a nonfiler for current year
	*******************************************************************************/

	g state_enum =1	if state == "AL" //Alabama
	replace state_enum =2	if state == "AK" //Alaska
	replace state_enum =3	if state == "AZ" //Arizona
	replace state_enum =4	if state == "AR" //Arkansas
	replace state_enum =5	if state == "CA" //California
	replace state_enum =6	if state == "CO" //Colorado
	replace state_enum =7	if state == "CT" //Connecticut
	replace state_enum =8	if state == "DE" //Delaware
	replace state_enum =9	if state == "DC" //DC
	replace state_enum =10	if state == "FL" //Florida
	replace state_enum =11	if state == "GA" //Georgia
	replace state_enum =12	if state == "HI" //Hawaii
	replace state_enum =13	if state == "ID" //Idaho
	replace state_enum =14	if state == "IL" //Illinois
	replace state_enum =15	if state == "IN" //Indiana
	replace state_enum =16	if state == "IA" //Iowa
	replace state_enum =17	if state == "KS" //Kansas
	replace state_enum =18	if state == "KY" //Kentucky
	replace state_enum =19	if state == "LA" //Louisiana
	replace state_enum =20	if state == "ME" //Maine
	replace state_enum =21	if state == "MD" //Maryland
	replace state_enum =22	if state == "MA" //Massachusetts
	replace state_enum =23	if state == "MI" //Michigan
	replace state_enum =24	if state == "MN" //Minnesota
	replace state_enum =25	if state == "MS" //Mississippi
	replace state_enum =26	if state == "MO" //Missouri
	replace state_enum =27	if state == "MT" //Montana
	replace state_enum =28	if state == "NE" //Nebraska
	replace state_enum =29	if state == "NV" //Nevada
	replace state_enum =30	if state == "NH" //NewHampshire
	replace state_enum =31	if state == "NJ" //NewJersey
	replace state_enum =32	if state == "NM" //NewMexico
	replace state_enum =33	if state == "NY" //NewYork
	replace state_enum =34	if state == "NC" //NorthCarolina
	replace state_enum =35	if state == "ND" //NorthDakota
	replace state_enum =36	if state == "OH" //Ohio
	replace state_enum =37	if state == "OK" //Oklahoma
	replace state_enum =38	if state == "OR" //Oregon
	replace state_enum =39	if state == "PA" //Pennsylvania
	replace state_enum =40	if state == "RI" //RhodeIsland
	replace state_enum =41	if state == "SC" //SouthCarolina
	replace state_enum =42	if state == "SD" //SouthDakota
	replace state_enum =43	if state == "TN" //Tennessee
	replace state_enum =44	if state == "TX" //Texas
	replace state_enum =45	if state == "UT" //Utah
	replace state_enum =46	if state == "VT" //Vermont
	replace state_enum =47	if state == "VA" //Virginia
	replace state_enum =48	if state == "WA" //Washington
	replace state_enum =49	if state == "WV" //WestVirginia
	replace state_enum =50	if state == "WI" //Wisconsin
	replace state_enum =51	if state == "WY" //Wyoming


	ren tax_famsize famsize
	ren  state state_str
	ren  state_enum state
	
	*povline and fpl
	do $code/calc_pov_line.do 
	ren famsize tax_famsize 


	*For filers exch_income is computed from AGI, non tax SS benfits, tax exempt intrest, untaxed foreign income, and agi of dependent filers 
	*For nonfilers exch_income is constructed per (Auten-Splinter 2022) from 1099-R/5498, W2, and 1099-Div/INT/MISC/G(for UI only)
	g calcfpl= round(exch_income/calc_pov_ln, .01) 
	replace calcfpl= 0 if calcfpl<0
	g calcfpl_100 = calcfpl*100
	
	*calculate ptc max using 2022-2023 applicable figures from IRC 36B (b)(3)(A)(iii) Temporary percentages for 2021 through 2025

	g payout_max_calcfpl = 1.001
	replace payout_max_calcfpl = .0	 	if calcfpl<1.00 & calcfpl!=.
	replace payout_max_calcfpl = .0	 	if calcfpl >=1.00	& calcfpl <1.33 & calcfpl!=.
	replace payout_max_calcfpl = .0		if calcfpl >=1.33 	& calcfpl <1.50 & calcfpl!=.
	replace payout_max_calcfpl = .0  		+ (calcfpl - 1.5)*(.020/.5) 	if calcfpl >=1.50 	& calcfpl <2.00 & calcfpl!=.
	replace payout_max_calcfpl = .020  	+ (calcfpl - 2.0)*(.020/.5) 	if calcfpl >=2.00 	& calcfpl <2.50 & calcfpl!=.
	replace payout_max_calcfpl = .040 	+ (calcfpl - 2.5)*(.020/.5) 	if calcfpl >=2.50 	& calcfpl <3.00 & calcfpl!=.
	replace payout_max_calcfpl = .060 	+ (calcfpl - 3.0)*(.025/1.0)   if calcfpl >=3.00 	& calcfpl <4.00 & calcfpl!=.
	replace payout_max_calcfpl = .085 	if calcfpl>=4.00 & calcfpl!=.

	*calculate repayment limits
	g singfil = single
	replace singfil = 1 if nonfiler & famsize_orig==1 

	g calc_repaylim = . //default unlimited
		
	replace calc_repaylim = 325  if $y==22 & singfil==1 & calcfpl<2.00
	replace calc_repaylim = 825  if $y==22 & singfil==1 & calcfpl>=2.00 & calcfpl<3.00 
	replace calc_repaylim = 1400 if $y==22 & singfil==1 & calcfpl>=3.00 & calcfpl<4.00 
	replace calc_repaylim = 650  if $y==22 & singfil==0 & calcfpl<2.00
	replace calc_repaylim = 1650 if $y==22 & singfil==0 & calcfpl>=2.00 & calcfpl<3.00 
	replace calc_repaylim = 2800 if $y==22 & singfil==0 & calcfpl>=3.00 & calcfpl<4.00 
									   
	replace calc_repaylim = 350  if $y==23 & singfil==1 & calcfpl<2.00
	replace calc_repaylim = 900  if $y==23 & singfil==1 & calcfpl>=2.00 & calcfpl<3.00 
	replace calc_repaylim = 1500 if $y==23 & singfil==1 & calcfpl>=3.00 & calcfpl<4.00 
	replace calc_repaylim = 700  if $y==23 & singfil==0 & calcfpl<2.00
	replace calc_repaylim = 1800 if $y==23 & singfil==0 & calcfpl>=2.00 & calcfpl<3.00 
	replace calc_repaylim = 3000 if $y==23 & singfil==0 & calcfpl>=3.00 & calcfpl<4.00 
	
	*calculate hypothetical PTC 
	g contrib_amt = payout_max_calcfpl* exch_income if exch_income>0
	replace exch_income = 0 if exch_income==. 
	
	g hyp_calc_ptc = min(prem_1095, max(slcsp_1095 - contrib_amt,0))
	g hyp_aptc_less_ptc = aptc_1095 - hyp_calc_ptc
	g hyp_trueup = 0 
	replace hyp_trueup = -hyp_aptc_less_ptc if hyp_aptc_less_ptc<0
	g hyp_excess = max(hyp_aptc_less_ptc,0)
	g hyp_repay = min(hyp_excess,calc_repaylim) 
	g hyp_unrecap = hyp_excess  - hyp_repay	

	
	/*Create a composite pct fpl measure that combines actual observed on the 8962 if it exists, with the calculated hypotheticals*/
	g comb_calcfpl = fpl_8962/100
	replace comb_calcfpl = calcfpl if fpl_8962==. | (f8962==0 & fpl_8962==0) //fill in the hypothecial calc fpl if it is missing
	replace comb_calcfpl = calcfpl if comb_calcfpl>4.00 & calcfpl>4.00
	
	/*generate fpl bins*/
	g fpl_bin  = 1 if comb_calcfpl<0.5  & comb_calcfpl!=.
	replace fpl_bin = 2  if comb_calcfpl>=0.5 & comb_calcfpl<=1.0 & comb_calcfpl!=.
	replace fpl_bin = 3  if comb_calcfpl>=1.0 & comb_calcfpl<=1.5 & comb_calcfpl!=.
	replace fpl_bin = 4  if comb_calcfpl>=1.5 & comb_calcfpl<=2.0 & comb_calcfpl!=.
	replace fpl_bin = 5  if comb_calcfpl>=2.0 & comb_calcfpl<=2.5 & comb_calcfpl!=.
	replace fpl_bin = 6  if comb_calcfpl>=2.5 & comb_calcfpl<=3.0 & comb_calcfpl!=.
	replace fpl_bin = 7  if comb_calcfpl>=3.0 & comb_calcfpl<=3.5 & comb_calcfpl!=.
	replace fpl_bin = 8  if comb_calcfpl>=3.5 & comb_calcfpl<=4.0 & comb_calcfpl!=.
	replace fpl_bin = 9  if comb_calcfpl>=4.0 & comb_calcfpl<=4.5 & comb_calcfpl!=.
	replace fpl_bin = 10 if comb_calcfpl>=4.5 & comb_calcfpl<=5.0 & comb_calcfpl!=.
	replace fpl_bin = 11 if comb_calcfpl>=5.0 & comb_calcfpl<=5.5 & comb_calcfpl!=.
	replace fpl_bin = 12 if comb_calcfpl>=5.5 & comb_calcfpl<=6.0 & comb_calcfpl!=.
	replace fpl_bin = 13 if comb_calcfpl>=6.0 & comb_calcfpl!=.
	

	*combined/composite variables that take into account hypothetical amounts
	g comb_aptc = aptc_8962 
	replace comb_aptc = aptc_1095 if comb_aptc==. | comb_aptc==0	//differs from aptc_1095 by the amount of APTC that reconcilers report. 
	
	*safe harbor for nonfilers and nonreconcilers
	g nonrec_unrecap = hyp_unrecap if (f8962==0) &(excess_unrecap_8962==0|excess_unrecap_8962==.) 
	g nonfiler_unrecap = hyp_unrecap if (f8962==.) &(excess_unrecap_8962==0|excess_unrecap_8962==.) 	
	
	*repayment amounts that are never actually repaid. 
	g nonrec_repay = hyp_repay if (f8962==0) &(repay_excess_aptc_8962==0|repay_excess_aptc_8962==.) 
	g nonfiler_repay = hyp_repay if (f8962==.) & (repay_excess_aptc_8962==0|repay_excess_aptc_8962==.)  //nonfiler repayment that is not repaid!
	
	*some general stats on this year's data 
	local f8962_vars = "aptc_8962 exch_cred_8962 repay_excess_aptc_8962 excess_unrecap_8962 ptc_net_8962"
	local f1095_vars = "aptc_1095"
	local comb_vars = "comb_aptc"
	local nonfiler = "nonrec_unrecap nonfiler_unrecap nonrec_repay nonfiler_repay"

	*replace zeros with missing to get the conditional means
	foreach var in `f8962_vars' `f1095_vars' `comb_vars' `nonfiler' { //don't get conditional ratios
		replace `var' = . if `var'==0
	}
	
	*get labels and count strings
	local assign_count_vars = ""
	local count_vars = ""
	foreach var in `f8962_vars' `f1095_vars' `comb_vars' `nonfiler'  {
		local count_vars = "`count_vars' n_`var' "
		local assign_count_vars = "`assign_count_vars' n_`var' = `var' "
	}
	
	di "Count vars are `count_vars'"
	
	*Collapse 
	collapse (count) n= ssn_p `assign_count_vars' ///
			 (sum) `f8962_vars' `f1095_vars' `comb_vars' `nonfiler' , ///
			 by(taxyr fpl_bin nonfiler nonrec)

	*replace missing with zeros to get the right totals
	foreach var in `f8962_vars' `f1095_vars' `comb_vars'  `nonfiler'  {
		replace `var' = 0 if `var'==.
	}
	
	tempfile collapsed`yy'
	save `collapsed`yy''

}




*Use appended data to create graph
clear 
forvalues yy = 22/23 {
	append using `collapsed`yy''
}

label define fplbin 1 "<50" 2 "50-100" 3 "100-150" 4 "150-200" 5 "200-250" 6 "250-300" 7 "300-350" 8 "350-400" 9 "400-450" 10 "450-500" 11 "500-550" 12 "550-600" 13 "600+" 
label values fpl_bin fplbin


save $output/collapsed_all.dta, replace 

*Figure 2  2022 and 2023
preserve
	collapse (sum) repay_excess_aptc_8962 	nonrec_repay 	nonfiler_repay  /// repayents including those that are never made...
		       excess_unrecap_8962 	nonrec_unrecap 	nonfiler_unrecap  /// safe harbor 
		       aptc_8962 ptc_net_8962 exch_cred_8962 comb_aptc aptc_1095 /// ptc & aptc
		       n_repay_excess_aptc_8962 n_nonrec_repay 	n_nonfiler_repay  /// count repayents 
		       n_excess_unrecap_8962 	n_nonrec_unrecap n_nonfiler_unrecap  /// count safe harbor 
		       n_aptc_8962 n_ptc_net_8962 n_exch_cred_8962 n_comb_aptc n_aptc_1095 /// count ptc& aptc       
		       , by(taxyr)

	g repay_signflip = -repay_excess_aptc_8962
	g total_safeharbor = excess_unrecap_8962 + nonrec_unrecap + nonfiler_unrecap 
	g aptc_minus_safe_unrecap_nonfile = aptc_1095 - nonrec_repay - nonfiler_repay - total_safeharbor
	g total_ptc = aptc_1095 - repay_excess_aptc_8962 + ptc_net_8962
	g failed_aptc = aptc_1095 - aptc_8962
	
	*graph broken out by components
	graph bar (sum) repay_signflip excess_unrecap_8962 nonrec_unrecap nonfiler_unrecap nonrec_repay nonfiler_repay aptc_minus_safe_unrecap_nonfile ptc_net_8962, ///
		over(taxyr, lab(angle(45))) stack ///
		legend(order(1 "Repayment" 2 "Safe Harb. (filer)" 3 "Safe Harb. (nonrec)" 4 "Safe Harb. (nonfiler)"  5 "Unpaid (nonrec)" 6 "Unpaid (nonfiler)" 7 "APTC less" 8 "True-Ups" )) 
		graph export $output/Fig2_components_byYear_22_23.pdf, replace
	
	export excel using "$output/Table_Fig2_components_byYear_22_23.xlsx" , firstrow(var) replace 
restore 




log close
